//
//  KLMMineController.swift
//  KLMHome
//
//  Created by Nic on 2025/2/17.
//

import UIKit

private let cellId = String(describing: UITableViewCell.self)

class KLMMineController: KLMBaseController {
    

    @IBOutlet weak var iconView: UIButton!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var detailLabel: UILabel!
    @IBOutlet weak var tipLabel: UILabel!
    @IBOutlet weak var dayLabel: UILabel!
    @IBOutlet weak var dayTipLabel: UILabel!
    
    @IBOutlet weak var tableView: UITableView!
    
    @IBOutlet weak var userView: UIView!
    @IBOutlet weak var dayView: UIView!
    
    
    let items: [String] = {
        [
            "家庭管理",
            "消息中心",
            "帮助中心"
        ]
    }()
    let imageNames: [String] = {
        [
            "setting_home",
            "setting_chat",
            "setting_help"
        ]
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.rightBarButtonItem = UIBarButtonItem.itemWithBaseButton(KLMBaseButton(image: "Nav_Setting"), target: self, action: #selector(tapSetting(_:)))
        view.backgroundColor = rgba(243, 243, 246, 1)
    }
    
    override func viewDidLoadConfig() {
        super.viewDidLoadConfig()
        
        nameLabel.configStyle(with: "ID / 昵称", font: UIFont(safeName: "PingFangSC-Regular", size: 16), titleColor: rgba(38, 38, 38, 1))
        detailLabel.configStyle(with: "账号信息、退出登陆", font: UIFont(safeName: "PingFangSC-Regular", size: 12), titleColor: rgba(138, 138, 138, 1))
        tipLabel.configStyle(with: "懂你之光已陪伴你", font: UIFont(safeName: "PingFangSC-Regular", size: 12), titleColor: rgba(129, 129, 129, 1))
        dayLabel.configStyle(with: "268", font: UIFont(safeName: "PingFangSC-Semibold", size: 32), titleColor: rgba(38, 38, 38, 1))
        dayTipLabel.configStyle(with: "天", font: UIFont(safeName: "PingFangSC-Regular", size: 12), titleColor: rgba(38, 38, 38, 1))
        
        iconView.backgroundColor = .clear
        
        userView.backgroundColor = .clear
        dayView.backgroundColor = rgba(146, 247, 197, 1)
        dayView.layer.cornerRadius = 8
        dayView.layer.masksToBounds = true
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        tableView.dataSource = self
        tableView.delegate = self
        
        tableView.separatorStyle = .none
        tableView.backgroundColor = .white
        tableView.layer.cornerRadius = 8
        tableView.layer.masksToBounds = true
        
        iconView.setImage(UIImage(named: "appLogo")?.croppedImage(with: 16, imgSize: iconView.size), for: .normal)
    }
    
    @IBAction func tapIcon(_ sender: UIButton) {
        let testVc = KLMTViewController()
        navigationController?.pushViewController(testVc, animated: true)
    }
    
    @objc func tapSetting(_ sender: UIButton) {
        
        let settingVc = KLMSettingController()
        settingVc.titleText = "设置"
        navigationController?.pushViewController(settingVc, animated: true)
    }
}


extension KLMMineController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        cell.imageView?.image = UIImage(named: imageNames[indexPath.row])
        cell.textLabel?.text = items[indexPath.row]
        cell.accessoryType = .disclosureIndicator

        return cell
    }
}

extension KLMMineController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        UIView.animate(withDuration: 1.0) {
            tableView.deselectRow(at: indexPath, animated: true)
        }
        if indexPath.row == 0 {
            let vc = KLMHomeManageController()
            vc.titleText = items[indexPath.row]
            navigationController?.pushViewController(vc, animated: true)
        }
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 64
    }
}
    
